package org.cos.generation.ast;

import java.util.LinkedList;
import java.util.List;

import org.cos.generation.epda.EPDAOptimizer;
import org.cos.generation.util.info.ClassInfo;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;


public class CoSASTAnalyzer {
	
	public static List<ClassInfo> analysis(List<ICompilationUnit> fileList) {
		List<ClassInfo> classInfoList = new LinkedList<ClassInfo>();
		// Analysis.
		for (ICompilationUnit cu : fileList) {
			classInfoList.addAll(analysis(cu));
		}
		// Squelch custom calls.
		EPDAOptimizer.customCallSquelch(classInfoList);
		// Reduce null transitions.
		EPDAOptimizer.nullTransReduce(classInfoList);
		// Return.
		return classInfoList;
	}
	
	public static List<ClassInfo> analysis(ICompilationUnit cu) {
		List<ClassInfo> classInfoList = new LinkedList<ClassInfo>();
		// Create a new parser.
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		// Set kind.
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
		// Set source.
		parser.setSource(cu); 
		// We need bindings later on.
		parser.setResolveBindings(true); 
		// Parse the source code and get the AST.
		CompilationUnit astUnit = (CompilationUnit) parser.createAST(null /* IProgressMonitor */);
		// Create a new visitor.
		CoSASTVisitor visitor = new CoSASTVisitor();
		// Visit the AST and return the EPDA generated.
		List<ClassInfo> classList = visitor.process(astUnit);
		// Get ClassInfo list.
		for (ClassInfo classInfo : classList) {
			classInfoList.add(classInfo);
		}
		// Return.
		return classInfoList;
	}
	
	public static void analysis(char[] sourceUnit) {
		// Create a new parser.
		ASTParser parser = ASTParser.newParser(AST.JLS3);
		// Set kind.
		parser.setKind(ASTParser.K_COMPILATION_UNIT);
		// Set source.
		parser.setSource(sourceUnit); 
		// We need bindings later on.
		parser.setResolveBindings(true); 
		// Parse the source code and get the AST.
		CompilationUnit astUnit = (CompilationUnit) parser.createAST(null /* IProgressMonitor */);
		// Create a new visitor.
		CoSASTVisitor visitor = new CoSASTVisitor();
		// Visit the AST and return the EPDA generated.
		visitor.process(astUnit);
	}
	
}
